﻿using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace RunSPMetal
{
    public partial class MainForm1 : Form
    {
        public MainForm1()
        {
            InitializeComponent();

            this.txtArguments.Text = string.Format("/web:{0} /code:{1} /namespace:SharePointDriver", "http://win2k8/", "SiteEntities.cs");
        }

        private void dorunspmetal_Click(object sender, EventArgs e)
        {
            
            
            //string path = @"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\spmetal.exe";
            
            string path = System.IO.Path.Combine(Microsoft.SharePoint.Utilities.SPUtility.GetGenericSetupPath(string.Empty), @"BIN\spmetal.exe");
            

            Process p = new Process();
            string output = null;

            try
            {
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.Arguments = this.txtArguments.Text;
                p.StartInfo.WorkingDirectory = @"C:\";
                p.StartInfo.FileName = path;
                p.StartInfo.RedirectStandardOutput = true;
                //p.StartInfo.CreateNoWindow = true;
                p.Start();
                // This code assumes the process you are starting will terminate itself. 
                // Given that is is started without a window so you cannot terminate it 
                // on the desktop, it must terminate itself or you can do it programmatically
                // from this application using the Kill method.

                // Do not wait for the child process to exit before
                // reading to the end of its redirected stream.
                // p.WaitForExit();
                // Read the output stream first and then wait.
                output = p.StandardOutput.ReadToEnd();
                p.WaitForExit();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if(this.viewoutput.Checked)
                    MessageBox.Show(output);

            }
        }
    }
}
